setwd("~/Dropbox/research/losingtouch/replication") # Remember to set this to your working directory

library(tidyverse)
library(udpipe)
library(furrr)
library(progressr)
library(patchwork)
library(viridisLite)
library(scales)

#read in text evaluations data
teval2 <- read_rds("textevals.rds")

vallogitlix <- glm(easier~lixrevrs+nwords,data=teval2,family="binomial")
vallogitrar <- glm(easier~rarrevrs+nwords,data=teval2,family="binomial")

#output latex table for each model using modelsummary()
models <- list(vallogitlix,vallogitrar)
modelnames <- c("LIX","Rare words")

summary(vallogitlix)
summary(vallogitrar)

#output table
modelsummary(list("LIX measure"=vallogitlix,"Rare words measure"=vallogitrar),output = "tables/valregtab.tex",
             stars=TRUE,coef_map = c("lixrevrs"="Simplicity (LIX)","rarrevrs"="Simplicity (Rare words)","nwords"="No. of words"),
             gof_omit = "BIC|AIC",
             title="Logistic regression models predicting text chosen as easier. \\label{tab:valregtab}")


vllix90 <- ggeffects::ggpredict(vallogitlix,terms="lixrevrs [0:100]",ci.lvl = .90) %>% 
  as_tibble() %>% 
  transmute(conf.low90=conf.low,conf.high90=conf.high)

valloglixprdf <- ggeffects::ggpredict(vallogitlix,terms="lixrevrs [0:100]") %>% 
  as_tibble() %>% 
  bind_cols(vllix90)

vlrar90 <- ggeffects::ggpredict(vallogitrar,terms="rarrevrs [0:100]",ci.lvl = .90) %>% 
  as_tibble() %>% 
  transmute(conf.low90=conf.low,conf.high90=conf.high)

vallograrprdf <- ggeffects::ggpredict(vallogitrar,terms="rarrevrs [0:100]") %>% 
  as_tibble() %>% 
  bind_cols(vlrar90)

ggplot(valloglixprdf,aes(x=x,y=predicted)) +
  geom_ribbon(aes(ymin=conf.low,ymax=conf.high),alpha=.2,fill=viridis(n=10)[6]) +
  geom_ribbon(aes(ymin=conf.low90,ymax=conf.high90),alpha=.2,fill=viridis(n=10)[6]) +
  geom_line(color=viridis(n=10)[3]) +
  theme_bw() +
  labs(x="Clarity",y="Pr(Selected as easier)") +
  ggtitle("LIX measure")

ggsave("figures/valplotlix.pdf",width=4,height=4)

ggplot(vallograrprdf,aes(x=x,y=predicted,ymin=conf.low,ymax=conf.high)) +
  geom_ribbon(aes(ymin=conf.low,ymax=conf.high),alpha=.2,fill=viridis(n=10)[6]) +
  geom_ribbon(aes(ymin=conf.low90,ymax=conf.high90),alpha=.2,fill=viridis(n=10)[6]) +
  geom_line(color=viridis(n=10)[3]) +
  theme_bw() +
  labs(x="Clarity",y="Pr(Selected as easier)") +
  ggtitle("Rare words measure")

ggsave("figures/valplotrar.pdf",width=4,height=4)






